module comm.comm;

import std.stdio;
import std.json;
import std.range : iota;
import  std.random;
import message.MsgDefine;
import message.Constants;
import std.conv : to;
import model;
import hunt.serialization;
import message.RedPacket;
import hunt.logging;

string EncodeJsonValue(ErrorObj obj)
{
    JSONValue root;
    root["code"] = obj.code;
    root["message"] = obj.message;
    root["data"] = obj.data;
    return root.toString;
}

string EncodeSendRedEnvelope(int code, int red_packet_id)
{
    JSONValue root;
    root["code"] = code;
    JSONValue data;
    root["message"] = "";
    data["red_packet_id"] = red_packet_id;
    root["data"] = data;
    return root.toString;
}

string EncodeGetRedEnvelope(RedEnvelope envObj,RedEnvelopeSplit splitObj,RedEnvelopeRecord[] records,ulong userId)
{
    RedPacket redpacket;
    if (envObj)
    {
      //红包本身的信息
      redpacket.red_packet_id = to!string(envObj.id);
      redpacket.user_id = to!string(envObj.fromId);
      redpacket.amount = envObj.amount;
      redpacket.count = envObj.number;
      redpacket.type = envObj.type;
      redpacket.wishes = envObj.message;
      redpacket.target_type = envObj.taget_type;
      redpacket.target_id = to!string(envObj.chatId);
      redpacket.created = envObj.created;
      redpacket.receive_count = to!int(records.length);
      redpacket.message_id = envObj.messageId;
      int len = to!int(records.length);
      redpacket.opened_duration = records.length == envObj.number ? (records[len-1].created - envObj.created) : 0;
    }

    Mine mine;

    mine.user_id = to!string(userId);
    logInfof("mine user : %s",userId);
    mine.amount = 0;

    Detailed[] detaileds;

    //把红包领取记录也放进去
    foreach (RedEnvelopeRecord r ; records)
    {
      Detailed d;
      d.id = to!string(r.id);
      d.user_id = to!string(r.userId);
      d.amount = r.dynamic;
      d.created = r.created;

      if(r.userId == userId)
      {
        mine.amount = r.dynamic;
        logInfof("mine: %s", mine.amount);
      }
      detaileds ~= d;
    }
    redpacket.detailed = detaileds;
    redpacket.mine = mine;

    JSONValue root;
    if (envObj is null) 
    {
      root["code"] = -1;
      root["message"] = "not find";
    }
    else {
      root["code"] = 0;
      root["message"] = "";

      auto json = toJson(redpacket);
      logInfof("json:%s",json);
      root["data"] = json;
    }
    return root.toString;

    //JSONValue root;
    //root["code"] = 0;
    //JSONValue data;
    //root["message"] = "";
    //data["red_packet_id"] = envObj.id;
    //data["user_id"] = envObj.fromId;
    //data["amount"] = envObj.amount;
    //data["count"] = envObj.number;
    //data["type"] = envObj.type;
    //data["wishes"] = envObj.message;
    //data["target_type"] = 0;
    //data["target_id"] = envObj.chatId;
    //data["created"] = envObj.created;
    //data["receive_count"] = records.length;
    //int len = records.length;
    //data["opened_duration"] = records.length == envObj.number ? (records[len-1].created - envObj.created) : 0;
    //
    //JSONValue mine;
    //mine["user_id"] = userId;
    //mine["amount"] = 0;
    //
    //JSONValue detailed;
    //
    //
    //foreach (RedEnvelopeRecord r ; records)
    //{
    //    JSONValue obj;
    //    obj["id"] = 0;
    //    obj["user_id"] = r.userId;
    //    obj["amount"] = r.dynamic;
    //    obj["amount"] = r.dynamic;
    //    if(r.userId == userId)
    //    {
    //      mine["amount"] = r.dynamic;
    //    }
    //    detailed.array ~= obj;
    //}
    //
    //data["mine"] = mine;
    //data["detailed"] = detailed;



}

string RPCResponseEncode(JSONValue json)
{
    JSONValue root;

    root["code"] = 0;
    root["message"] = "";
    root["data"] = json;

    return root.toString();
}

SendRedEnvelopeReq DecodeSendRedEnvelope(string json)
{
    SendRedEnvelopeReq  req;
    JSONValue j = parseJSON(json);
    req.type = cast(int)j["type"].integer;
    req.amount = j["amount"].integer;
    req.wishes = j["wishes"].str;
    req.target_type = cast(int)j["target_type"].integer;
    req.target_id = to!int(j["target_id"].str);
    req.count = cast(int)j["count"].integer;
    req.message_id = j["message_id"].str;
    return req;
}

string EncodeRedEnvelope(string json, uint red_packet_id)
{
    JSONValue j = parseJSON(json);
    j["red_packet_id"] = red_packet_id;
    return j.toString;
}

GetRedEnvelopeReq DecodeGetRedEnvelope(string json)
{
    GetRedEnvelopeReq req;
    JSONValue j = parseJSON(json);
    req.red_packet_id = to!int(j["red_packet_id"].str);
    return req;
}


long[] averageSplitRedPacket(long money, int n)
{
  long[] array = new long[n];
  long ave = money/n;
   for(int i = 0 ;i < n ; ++i)
   {
      array[i] += ave;
   }
  return array;
}


long[] SplitRedPacket(long money, int n)
{
  long fen = money;
  if (fen < n || fen < 1)
  {
    writefln("error");
  }

  long[] array = new long[n];
  for(int i = 0 ;i < n ; ++i)
  {
    array[i] += 1;
  }
  fen -= n;

  int i = 0;

  while(fen > 1)
  {
    auto rnd = Random(unpredictableSeed);
    int f = uniform(0, cast(int)fen, rnd);
    array[i++%n] +=  f;
    fen -= f;
  }

  if (fen > 0){
    array[0] +=  fen;
  }

  return array;
}
